package ip.ejb.session.facade;
/***********************************************************************
 * Module:  CountryFacadeBean.java
 * Author:  mojsilo
 * Purpose: Defines the Class CountryFacadeBean
 ***********************************************************************/

import ip.dto.CountryDTO;
import ip.ejb.entity.CountryLocal;
import ip.ejb.entity.CountryLocalHome;
import ip.ejb.entity.CountryPK;
import ip.exceptions.CanNotCreateNewCountryException;
import ip.exceptions.NoSuchCountryException;
import ip.util.EJBHomeFactory;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.FinderException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.NamingException;

import org.apache.log4j.Logger;
/**
 * @ejb.bean
 *   name="CountryFacade"
 *   type="Stateless"
 *   jndi-name="ip/ejb/facade/CountryFacade"
 *   local-jndi-name="ip/ejb/facade/CountryFacadeLocal"
 * @ejb.transaction type="Supports"
 */
public class CountryFacadeBean implements SessionBean
{

	private static final long serialVersionUID = 1L;
	private SessionContext ctx;
	private CountryLocalHome countryLocalHome = null;
	private CountryLocal countryLocal = null;	
	private CountryPK countryPK = null;
	private Logger logger = Logger.getLogger(this.getClass());

	/**
	 * @throws CanNotCreateNewCountryException 
	 * @ejb.interface-method view-type="local"
	 */
	public void createCountry(CountryDTO countryDTO) throws CanNotCreateNewCountryException{
		try {
			countryLocalHome = (CountryLocalHome) EJBHomeFactory.getInstance().createLocalHome(ip.ejb.entity.CountryLocalHome.class);
			countryLocalHome.create(countryDTO.getName()); 

		} catch (NamingException e) {
			e.printStackTrace();
		} catch (CreateException e) {			
			e.printStackTrace();
			throw new CanNotCreateNewCountryException(countryDTO);
		}

	}

	/**
	 * @throws NoSuchCountryException 
	 * @ejb.interface-method view-type="local"
	 */
	public void updateCountry(CountryDTO countryDTO) throws NoSuchCountryException{
		try {
			countryPK = new CountryPK();
			countryPK.setCountryId(countryDTO.getCountryId());		
			countryLocalHome = (CountryLocalHome) EJBHomeFactory.getInstance().createLocalHome(ip.ejb.entity.CountryLocalHome.class);
			countryLocal = countryLocalHome.findByPrimaryKey(countryPK);

			countryLocal.setName(countryDTO.getName());

		} catch (NamingException e) {
			e.printStackTrace();
		} catch (FinderException e) {
			e.printStackTrace();
			throw new NoSuchCountryException(countryDTO);
		}
	}

	/**
	 * @throws NoSuchCountryException 
	 * @ejb.interface-method view-type="local"
	 */
	public CountryDTO getCountry(CountryDTO countryDTO) throws NoSuchCountryException{
		try {
			//there is possibility that there is no needed for this method
//			CountryLocalHome = (CountryLocalHome) EJBHomeFactory.getInstance().create("ip/ejb/entity/CountryLocal");
			countryLocalHome = (CountryLocalHome) EJBHomeFactory.getInstance().createLocalHome(ip.ejb.entity.CountryLocalHome.class);
			countryPK = new CountryPK();
			countryPK.setCountryId(countryDTO.getCountryId());
			countryLocal = countryLocalHome.findByPrimaryKey(countryPK);
			countryDTO = createCountryDTO(countryLocal);

		} catch (NamingException e) {
			e.printStackTrace();
		} catch (FinderException e) {
			e.printStackTrace();
			throw new NoSuchCountryException(countryDTO);
		}
		return countryDTO;

	}

	/**
	 * 
	 * @ejb.interface-method view-type="local"
	 */
	public Collection getAllCountries(){
		Collection countriesDTO = null;
		try {
//			countryLocalHome = (CountryLocalHome) EJBHomeFactory.getInstance().create("ip/ejb/entity/CountryLocal");
			countryLocalHome = (CountryLocalHome) EJBHomeFactory.getInstance().createLocalHome(ip.ejb.entity.CountryLocalHome.class);
			Collection countries = countryLocalHome.findAll();
			logger.debug("getAllCountries() - countries: "+countries.size());
			if(countries.size()>0){
				countriesDTO = new ArrayList();
				Iterator iter = countries.iterator();

				while (iter.hasNext()) {
					countryLocal = (CountryLocal) iter.next();
					countriesDTO.add(createCountryDTO(countryLocal));
				}
			}

		} catch (NamingException e) {
			e.printStackTrace();
		} catch (FinderException e) {
			e.printStackTrace();			
		}
		return countriesDTO;
	}

	/**
	 * 
	 * @ejb.interface-method view-type="local"
	 */
	public CountryDTO createCountryDTO(CountryLocal countryLocal) {			
		CountryDTO countryDTO = null;
		if(countryLocal != null){
			countryDTO = new CountryDTO();
			countryDTO.setCountryId(countryLocal.getCountryId());
			countryDTO.setName(countryLocal.getName());
		}
		return countryDTO;
	}

	/**
	 * @ejb.create-method
	 */
	public void ejbCreate(){}
	public void ejbActivate(){}
	public void ejbPassivate(){}
	public void ejbRemove(){}
	public void setSessionContext(SessionContext ctx) throws EJBException, RemoteException {
		this.ctx = ctx;
	}

}